import router from '@/router'
import store from '@/store'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({ showSpinner: false })
const whiteList = ['/login', '/404']
// 路由前置守卫
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  //   next()
  const token = store.getters.token
  if (token) {
    // 判断是不是去登录页
    if (to.path === '/login') {
      NProgress.done()
      next('/') // 引导到首页
    } else {
      if (!store.state.user.userInfo.userId) {
        const res = await store.dispatch('user/getUserInfo')
        console.log('权限管理需要用到的数据', res)
      }
      next() // 放行
    }
  } else {
    //   没登录白名单
    if (whiteList.includes(to.path)) {
    //   没登录 去白名单
      next()
    } else {
      NProgress.done()
      //   没登录 去登录页
      next('/login')
    }
  }
})
// 后置守卫
router.afterEach((to, from) => {
  NProgress.done()
})
